package basic.commons.tag;

import java.io.IOException;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class PageTag extends TagSupport {
	private Integer currentPage;
	private Integer pageSize;
	private Integer totalPage;
	private String url;

	@Override
	public int doStartTag() {
		JspWriter out = pageContext.getOut();
		
		StringBuffer sb=new StringBuffer();
		sb.append("<div>");
		//创建form
		sb.append("<form id=\"pageform\" action=\""+url+"\" method=\"post\">");
		sb.append("<input type=\"hidden\" name=\"page.currentPage\" id=\"currentPage\" value=\""+currentPage+"\" />");
		sb.append("</form>");
		//创建显示内容
		sb.append("<div>");
		sb.append("总页数："+totalPage+" ");
		sb.append("<input type=\"text\" id=\"topage\"  />");
		sb.append("<input type=\"button\" onclick=\"pageChanges('2')\" value=\"确定\" />");
		
		if(currentPage>1){
			sb.append("<a onclick=\"pageChanges('"+(currentPage-1)+"','1')\">上一页</a>");
		}
		if(currentPage<totalPage){
			sb.append("<a onclick=\"pageChanges('"+(currentPage+1)+"','1')\">下一页</a>");
		}
		sb.append("</div>");
		
		sb.append("</div>");
		
		//创建点击方法
		sb.append("<script type=\"text/javascript\">");
		sb.append("function pageChanges(currentPage,type){");
		sb.append("if(type==1){");
		sb.append("$(\"#currentPage\").val(currentPage);");
		sb.append("}else{");
		sb.append("$(\"#currentPage\").val($(\"#topage\").val());");
		sb.append("}");
		sb.append("$(\"#pageform\").submit();");
		sb.append("}");
		sb.append("</script>");
		
		try {
			out.print(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 1;
	}
	
	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Integer getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}
	
}
